package com.xbongbong.saas.help;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.elasticsearch.pojo.XbbAggregatedPage;
import com.xbongbong.paas.elasticsearch.util.EsUtil;
import com.xbongbong.paas.elasticsearch.util.XbbElasticsearchRestTemplate;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.constant.ElasticConstant;
import com.xbongbong.saas.enums.SheetAloneEnum;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;

/**
 * @author 吴峰
 * @date 2021/08/04 15:31
 */
@Component
public class SaasFundHelp {

    @Resource
    private XbbElasticsearchRestTemplate xbbElasticsearchRestTemplate;
    /**
     * 根据uuid、alone获取回款单
     * @param indexTypeEnum 业务es索引枚举
     * @param corpid 公司id
     * @param sheetAloneEnum 要查询的父子类型
     * @param uuid 回/付款单uuid（父的回/付款单的uuid）
     * @param orderStr 需要排序的字段
     * @return java.util.List<com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt>
     * @throws XbbException
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    public List<PaasFormDataEntityExt> getEsDataListByUuidAndAlone(IndexTypeEnum indexTypeEnum, String corpid, SheetAloneEnum sheetAloneEnum, String uuid, String orderStr) throws XbbException {
        if (StringUtil.isEmpty(uuid)) {
            return new ArrayList<>();
        }
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        boolQueryBuilder.filter(termQuery(FieldTypeEnum.SHEET_UUID.getAlias() + ".keyword", uuid));
        boolQueryBuilder.filter(termQuery("del", 0));
        //只获取子回款单，如果alone的值是PaymentSheetAloneEnum.NORMAL，则其本身就是自己的明细，无需查询明细
        boolQueryBuilder.filter(termQuery(FieldTypeEnum.SHEET_ALONE.getAlias(), sheetAloneEnum.getCode()));
        boolQueryBuilder.filter(termQuery(ElasticConstant.MY_JOIN_FIELD,indexTypeEnum.getType()));
        sourceBuilder.query(boolQueryBuilder);
        SearchRequest searchRequest = new SearchRequest(indexTypeEnum.getIndex());
        if(Objects.nonNull(orderStr)){
            sourceBuilder.sort(new FieldSortBuilder(orderStr).order(SortOrder.ASC));
        }
        PageRequest pageRequest = EsUtil.setPage(sourceBuilder, 0, PaasConstant.ES_MAX_PAGE_SIZE);
        searchRequest.source(sourceBuilder);
        XbbAggregatedPage<PaasFormDataEntityExt> esEntities = xbbElasticsearchRestTemplate.queryForPages(pageRequest, searchRequest, PaasFormDataEntityExt.class);
        return esEntities.getContent();
    }
}
